<?php
declare (strict_types = 1);

namespace app\controller;

use app\service\CategoryService;
use app\service\GoodsService;
use app\service\HouseService;
use app\service\UserService;
use think\facade\Validate;
use think\Request;
use app\common\BaseController;

class CategoryController extends BaseController
{
  /**
   * 显示资源列表
   *
   * @return \think\Response
   */
  public function index(Request $request)
  {
    $sellable = $request->param('sellable',false);
    $isHouse = $request->param('isHouse',0);
    $process = $request->param('process','');
    $base = $request->param('base', 0);
    // 获取分类
    $category = [];
    $conditions = [];
    $conditions[] = ['base', $base, '='];
    $order = ['id' => 'ASC'];
    $data = CategoryService::getInstance()->getCategoryByCondition($conditions, $order);

    // 获取总部分类
    if (!$base) {
      $cons = ['g.status', 1, '='];
      $check = 0;
      // 商品原料或成品
      if ($process !== '') {
        $cons[] = ['g.process', $process, '='];
        $check = 1;
      }
      if ($sellable) {
        $cons[] = ['g.sellable', 1, '='];
        $cons[] = ['g.price', 0, '>'];
        $check = 1;
      }
      if ($isHouse) {
        $cons[] = ['h.count', 0, '>'];
        $check = 1;
      }
      $goods = GoodsService::getInstance()->getGoodsHouse($cons);
      $catId = array_unique(array_column($goods, 'category_id'));
      foreach ($data as $value) {
        if ($check && !in_array($value['id'], $catId)) {
          continue;
        }
        $category[] = $value;
      }
    // 获取门店分类
    } else {
      $category = $data;
    }
    return $this->send200Response($category);
  }

  /**
   * 保存新建的资源
   *
   * @param  \think\Request  $request
   * @return \think\Response
   */
  public function save(Request $request)
  {
    $categoryName = $request->param('categoryName','');
    $base = $request->param('base',0);
    if (!$categoryName || !trim($categoryName)) {
      return $this->send400Response('分类不能为空');
    }
    // 新增门店分类
    if ($base === 0) {
      // 查询是否已有分类
      $data = CategoryService::getInstance()->getCategoryByCondition([['category_name', trim($categoryName), '='], ['base', $base, '='], ['status', 1, '=']]);
      if ($data) {
        return $this->send400Response('该分类已存在');
      }
      // 查询有此分类的门店
      $haveStore = [];
      $haveCat = CategoryService::getInstance()->getCategoryByCondition([['category_name', trim($categoryName), '='], ['status', 1, '=']]);
      if ($haveCat) {
        $haveStore = array_column($haveCat, 'base');
      }
      // 需要添加分类的base
      $addStore = [0];
      $baseList = UserService::getInstance()->storeAll();
      $baseList = array_column($baseList, 'base');
      $addStore = array_merge($addStore, array_diff($baseList, $haveStore));
      // 新增总部和门店此分类
      $result = CategoryService::createCategory($categoryName, $addStore);
      if ($result['status']) {
        return $this->send200Response('ok');
      } else {
        return $this->send400Response($result['msg']);
      }

    }
    return $this->send200Response('ok');

  }
}